﻿namespace EyeSoft.Wpf.Facilities
{
	using System;
	using System.Windows;

	public static class TaskContinue
	{
		private static readonly object lockInstance = new object();

		private static bool useDispatcher = true;

		private static bool initialized;

		public static void UseDispatcher(bool use = true)
		{
			lock (lockInstance)
			{
				if (initialized)
				{
					new InvalidOperationException("The UseDispatcher method can be called only one time.")
						.Throw();
				}

				useDispatcher = use;

				initialized = true;
			}
		}

		public static void Execute(Action action)
		{
			if (useDispatcher)
			{
				Application.Current.Sync().Execute(action);
				return;
			}

			action();
		}
	}
}